/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.domain;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ripper
 */
public class Animation 
{
    private List<Frame> frames;
    private long animationStart;
    private int totalAnimationLength;
    
    public Animation()
    {
        frames = new ArrayList<Frame>();
        totalAnimationLength = 0;
    }
    
    public void addFrame(Frame f)
    {
        frames.add(f);
        totalAnimationLength = 0;
        for(Frame ff: frames)
        {
            totalAnimationLength += ff.getTimeDrawn();
        }
    }
    
    public void startAnimation()
    {
        animationStart = System.currentTimeMillis();
    }
    
    public BufferedImage getImage()
    {
        long timePassed = System.currentTimeMillis() - animationStart;
        timePassed = timePassed % totalAnimationLength;
        
        int tp = 0;
        int index = -1;
        while(tp <= timePassed)
        {
            index++;
            Frame f = frames.get(index);
            tp += f.getTimeDrawn();
        }
        
        return frames.get(index).getImage();
    }

    public List<Frame> getFrames() {
        return frames;
    }
}
